package com.egao.common.manage.school;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.module.company.entity.Company;
import com.egao.common.module.company.entity.vo.FieldFormVO;
import com.egao.common.module.company.entity.vo.FieldListVO;
import com.egao.common.module.company.entity.vo.FieldRecordFormVO;
import com.egao.common.module.company.service.CompanyService;
import com.egao.common.module.company.service.FieldRecordService;
import com.egao.common.module.company.service.FieldService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 场地管理
 * @author dl
 */
@Api(tags = "场地管理")
@RestController
@RequestMapping("${api}")
public class SchoolFieldController extends BaseController {

    @Autowired
    private FieldService fieldService;

    @Autowired
    private FieldRecordService fieldRecordService;

    @Autowired
    private CompanyService companyService;

    @PreAuthorize("hasAuthority('school:field:list')")
    @ApiOperation(value = "场地管理", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "场地管理", desc = "分页查询招聘信息")
    @ApiPageParam
    @GetMapping("/school/field/list1")
    public JsonResult list(HttpServletRequest request) {
        PageParam<FieldListVO> pageParam = new PageParam<>(request);
        return fieldService.list(pageParam);
    }

    @PreAuthorize("hasAuthority('school:field:list')")
    @ApiOperation(value = "场地管理", notes = "未审核状态升序 创建时间倒序")
    @OperLog(value = "场地管理", desc = "分页查询招聘信息")
    @ApiPageParam
    @GetMapping("/school/field/list")
    public JsonResult queryList(HttpServletRequest request) {
        PageParam<FieldListVO> pageParam = new PageParam<>(request);
        return fieldService.queryList(pageParam);
    }

    @PreAuthorize("hasAuthority('school:field:save')")
    @OperLog(value = "添加场地信息", desc = "添加场地信息", result = true)
    @ApiOperation(value = "添加场地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldFormVO", value = "fieldFormVO", required = true, dataType = "FieldFormVO")
    })
    @PostMapping("/school/field/save")
    public JsonResult save(@Valid @RequestBody FieldFormVO fieldFormVO) {
        return fieldService.saveField(fieldFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:update')")
    @OperLog(value = "修改场地信息", desc = "修改场地信息", result = true)
    @ApiOperation(value = "修改场地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldFormVO", value = "fieldFormVO", required = true, dataType = "FieldFormVO")
    })
    @PutMapping("/school/field/update")
    public JsonResult update(@Valid @RequestBody FieldFormVO fieldFormVO) {
        return fieldService.updateField(fieldFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:delete')")
    @OperLog(value = "删除场地信息", desc = "删除场地信息", result = true)
    @ApiOperation(value = "删除场地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldFormVO", value = "fieldFormVO", required = true, dataType = "FieldFormVO")
    })
    @PutMapping("/school/field/delete")
    public JsonResult delete(@RequestBody FieldFormVO fieldFormVO) {
        return fieldService.deleteField(fieldFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:detail')")
    @ApiOperation(value = "获取场地信息详情")
    @OperLog(value = "获取场地信息详情", desc = "获取场地信息详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "场地信息id", required = true, dataType = "int")
    })
    @GetMapping("/school/field/detail")
    public JsonResult detail(Integer id) {
        return fieldService.detail(id);
    }

    @PreAuthorize("hasAuthority('school:field:apply')")
    @ApiOperation(value = "锁定/解锁场地")
    @OperLog(value = "锁定/解锁场地", desc = "锁定/解锁场地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldRecordFormVO", value = "fieldRecordFormVO", required = true, dataType = "FieldRecordFormVO")
    })
    @PutMapping("/school/field/apply")
    public JsonResult apply(@RequestBody FieldRecordFormVO fieldRecordFormVO) {
        return fieldRecordService.saveFieldRecord(fieldRecordFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:apply')")
    @ApiOperation(value = "锁定场地")
    @OperLog(value = "锁定场地", desc = "锁定场地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldRecordFormVO", value = "fieldRecordFormVO", required = true, dataType = "FieldRecordFormVO")
    })
    @PutMapping("/school/field/lock")
    public JsonResult lock(@RequestBody FieldRecordFormVO fieldRecordFormVO) {
        return fieldRecordService.lockFieldRecord(fieldRecordFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:apply')")
    @ApiOperation(value = "解锁场地")
    @OperLog(value = "解锁场地", desc = "解锁场地")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldRecordFormVO", value = "fieldRecordFormVO", required = true, dataType = "FieldRecordFormVO")
    })

    @PutMapping("/school/field/unlock")
    public JsonResult unlock(@RequestBody FieldRecordFormVO fieldRecordFormVO) {
        return fieldRecordService.deleteFieldRecord(fieldRecordFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:delete-record')")
    @OperLog(value = "删除场地信息", desc = "删除场地信息", result = true)
    @ApiOperation(value = "删除场地信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fieldRecordFormVO", value = "fieldRecordFormVO", required = true, dataType = "FieldRecordFormVO")
    })
    @PutMapping("/school/field/delete-record")
    public JsonResult deleteFieldRecord(@RequestBody FieldRecordFormVO fieldRecordFormVO) {
        return fieldRecordService.deleteFieldRecord(fieldRecordFormVO);
    }

    @PreAuthorize("hasAuthority('school:field:list')")
    @ApiOperation(value = "查询场地", notes = "查询场地")
    @OperLog(value = "查询场地", desc = "查询场地")
    @ApiPageParam
    @GetMapping("/school/field/query")
    public JsonResult query(String holdTime, String type) {
        return fieldService.queryField(holdTime, type);
    }

    @ApiOperation(value = "查询场地")
    @GetMapping("/school/field/company")
    public JsonResult company() {
        List<Company> list = companyService.list();
        List<Map<String, Object>> mapList = new ArrayList<>();
        for (Company company : list) {
            Map<String, Object> map = new HashMap<>();
            map.put("label", company.getName() + "(" + company.getOrganizationCode() + ")");
            map.put("value", company.getId());
            mapList.add(map);
        }
        return JsonResult.ok().setData(mapList);
    }
}
